using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Geomatrix
{
    class Button
    {
        bool _pressed = false;
        Vector2 _position;
        Texture2D _butTex;
        private int _width;
        private int _height;
        String _str;

        //Constructor for Button
        public Button(Vector2 pos, Texture2D butTex, String str)
        {
            _position = pos;
            _butTex = butTex;
            _width = 200;
            _height = 32;
            _str = str;
        }

        public bool Update(TouchCollection tc)
        {
            _pressed = false;

            foreach (TouchLocation tl in tc)
            {
                //Check if in bounds:
                if (tl.Position.X > _position.X && tl.Position.X < _position.X + _width)
                {
                    if (tl.Position.Y > _position.Y && tl.Position.Y < _position.Y + _height)
                    {
                        _pressed = true;
                        if (tl.State == TouchLocationState.Released)
                            return true; //Return true if button is released.
                    }
                }
            }

            return false;
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(_butTex, _position, (_pressed?Color.Lime:Color.Green));

            //Center Text
            Vector2 FontOrigin = Game1.buttonFont.MeasureString(_str) / 2;

            sb.DrawString(Game1.buttonFont, _str, _position + new Vector2(_width / 2, _height / 2), Color.White, 0,
                                    FontOrigin, 1, 0, 0);    
        }

        public Vector2 GetPosition()
        {
            return _position;
        }

        public void SetPosition(Vector2 newPos)
        {
            _position = newPos;
        }
    }
}